package com.infinitus.autocatlog.web;


import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.infinitus.autocatlog.service.KafkaTestSender;

/**
 * 
 */
@Controller
@RequestMapping("/cat-log-web")
public class CatLogWeb {
	
	@RequestMapping(value="/", method=RequestMethod.POST)
	@ResponseBody 
	public Object getList( HttpServletRequest request,String jsonStr ) {  
		System.out.println("--------接收到数据----------"+jsonStr);
		Map<String, String> resMap =  new HashMap<String, String>();
		if(jsonStr == null || "".equals(jsonStr.trim())){
			resMap.put("status", "error");
			resMap.put("reson", "jsonStr is blank");
			return resMap;
		}
		try{
			JSONObject json = JSONObject.parseObject(jsonStr);
			/*new Thread(new Runnable() {
				public void run() {
					String msg = null;
					KafkaSender.getIntance().send(msg);
				}
			}).start();*/
		}catch(Exception e){
			resMap.put("status", "error");
			resMap.put("reson", "cannot resolve jsonStr");
		}
		return resMap;
	}

	/*@RequestMapping("/") 
	@ResponseBody
	public String getList(
			HttpServletRequest request,
			@RequestParam(value = "EventScreen", required = false) String EventScreen,
			@RequestParam(value = "EventAction", required = false) String EventAction,
			@RequestParam(value = "EventLabel", required = false) String EventLabel,
			@RequestParam(value = "EventValue", required = false) String EventValue,
			@RequestParam(value = "EventDateTime", required = false) String EventDateTime,
			@RequestParam(value = "SessionID", required = false) String SessionID,
			@RequestParam(value = "UserID", required = false) String UserID,
			@RequestParam(value = "ClientDeviceID", required = false) String ClientDeviceID,
			@RequestParam(value = "ClientDeviceModel", required = false) String ClientDeviceModel,
			@RequestParam(value = "SystemName", required = false) String SystemName,
			@RequestParam(value = "ClientDeviceBrand", required = false) String ClientDeviceBrand,
			@RequestParam(value = "SystemVersion", required = false) String SystemVersion,
			@RequestParam(value = "ClientDeviceVersion", required = false) String ClientDeviceVersion,
			@RequestParam(value = "OsScreen", required = false) String OsScreen,
			@RequestParam(value = "ClientDeviceAgent", required = false) String ClientDeviceAgent,
			@RequestParam(value = "network", required = false) String network,
			@RequestParam(value = "s", required = false) String s,
			// 新加h5上报字段
			@RequestParam(value = "scene", required = false) String scene,
			@RequestParam(value = "module", required = false) String module,
			@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "button", required = false) String button,
			@RequestParam(value = "value", required = false) String value
	) {
		System.out.println("---------cat-log-web---in---------------");
		Map params = request.getParameterMap();
		System.out.println(params);
		System.out.println("--------需要处理的数据集----------------------");
		System.out.println("EventScreen            :" + EventScreen + "\n"
				+ "EventAction            :" + EventAction + "\n"
				+ "EventLabel             :" + EventLabel + "\n"
				+ "EventValue             :" + EventValue + "\n"
				+ "EventDateTime          :" + EventDateTime + "\n"
				+ "SessionID              :" + SessionID + "\n"
				+ "UserID                 :" + UserID + "\n"
				+ "ClientDeviceID         :" + ClientDeviceID + "\n"
				+ "ClientDeviceModel      :" + ClientDeviceModel + "\n"
				+ "SystemName             :" + SystemName + "\n"
				+ "ClientDeviceBrand      :" + ClientDeviceBrand + "\n"
				+ "SystemVersion          :" + SystemVersion + "\n"
				+ "ClientDeviceVersion    :" + ClientDeviceVersion + "\n"
				+ "OsScreen               :" + OsScreen + "\n"
				+ "ClientDeviceAgent      :" + ClientDeviceAgent + "\n"
				+ "network                :" + network + "\n"
				+ "s                      :" + s + "\n"
				+ "scene                  :" + scene + "\n"
				+ "module                 :" + module + "\n"
				+ "page                   :" + page + "\n"
				+ "button                 :" + button + "\n"
				+ "value                  :" + value + "\n"

		);
		Properties props = new Properties();
		props.put(
				"bootstrap.servers",
				"hrtwksvr01.infinitus.com.cn:6667,hrtwksvr02.infinitus.com.cn:6667,hrtwksvr03.infinitus.com.cn:6667");
		props.put("acks", "all");
		// 消息发送失败后的重试次数
		props.put("retries", "0");
		// 消息批处理大小
		props.put("batch.size", "16384");
		props.put("linger.ms", "1");
		props.put("buffer.memory", "33554432");
		props.put("key.serializer",
				"org.apache.kafka.common.serialization.StringSerializer");
		props.put("value.serializer",
				"org.apache.kafka.common.serialization.StringSerializer");
		// 初始化生产端

		Producer<String, String> producer = new KafkaProducer<String, String>(
				props);
		for (int i = 0; i < 1024; i++) {
			// 开始发送消息到指定topic
			producer.send(new ProducerRecord<String, String>(topic,
					"Hello! kafka"));
		}
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONObject jo = new JSONObject();
		jo.put("resp_code", "00000");
		jo.put("resp_desc", "OK");
		return jo.toJSONString();
	}*/
}
